package com.controller;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.StaticMapUtil;
import com.Util;
import com.aliyun.oss.OSSClient;
import com.client.HttpClientService;

/** 
* @author  Jrxsr
* @version 2018年10月30日
* 雇主中心的请求
*/

@Controller
@Scope("prototype")
public class IController {
	
	static Logger log = Logger.getLogger(IController.class);
//	Jedis jedis;
	public IController() {
		log.error("IController--构造了");
		//this.sessionId="9527";
	}
	@Autowired
	HttpClientService httpClientServiceImpl;
	private static String serverIP;

	public static void setServerIP(String serverIP) {
		// 修改IP可以通过修改redis数据库serverIP的值修改
		IController.serverIP = serverIP;
		log.error(serverIP);
	}

	// 连接服务器的方法
	public String httppost(String method, Map map ,HttpServletRequest request) throws Exception {
		try {
			//String str = request.getSession().getId();
			String str = httpClientServiceImpl.doPost(serverIP+method,map);
			log.error("方法："+method+"---返回结果："+str);
			return str;
		}catch (Exception e) {
			log.error("服务器连接超时。异常信息："+e.toString());
			return "服务器连接超时。异常信息："+e.toString();
		}
	}
	
//	private String sessionId=null;
//	
//	public void setSessionId(String sessionId) {
//		this.sessionId = sessionId;
//	}

	// 获取个人信息
	@RequestMapping(value="/getusermsg",method=RequestMethod.POST,produces = "application/json; charset=utf-8")
	@ResponseBody
	public String getusermsg(@RequestParam Map<String,Object> map, HttpServletRequest request) throws Exception {
		if(StaticMapUtil.getmap().get("session")!=null) {
			map.put("sessionId", StaticMapUtil.getmap().get("session"));
		}else {
			map.put("cuid", "9527");
		}
		return this.httppost("userinfo", map ,request);
	}
	// 上传图片 修改头像
	@RequestMapping(value="/upload",method=RequestMethod.POST,produces = "application/json; charset=utf-8")
	@ResponseBody
	public String upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws Exception {
		// 自定义一个图片名字
		String imgurl=Util.get32UUID()+".jpg";
		// Endpoint以杭州为例，其它Region请按实际情况填写。
		String endpoint = "http://oss-cn-beijing.aliyuncs.com";
		// 云账号AccessKey有所有API访问权限，建议遵循阿里云安全最佳实践，创建并使用RAM子账号进行API访问或日常运维，请登录
		// https://ram.console.aliyun.com 创建。
		String accessKeyId = "LTAIOh8vGGKueGvN";
		String accessKeySecret = "Xo5xrGQ0myG9yDpS9oDItqbZfRCaWo";

		// 创建OSSClient实例。
		@SuppressWarnings("deprecation")
		OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);

		// 上传文件流。
		InputStream ips = file.getInputStream();

		try {
			ossClient.putObject("erin099", imgurl, ips);
			// 关闭OSSClient。
			ossClient.shutdown();
		} catch (Exception e) {
			log.error("错误");
		}
		// 存入数据库的路径
		String fileName="https://erin099.oss-cn-beijing.aliyuncs.com/"+imgurl;
		
		Map<String,Object> map = new HashMap<String,Object>();
		if(StaticMapUtil.getmap().get("session")!=null) {
			map.put("sessionId", StaticMapUtil.getmap().get("session"));
		}else {
			map.put("cuid", "9527");
		}
		map.put("cuimg", fileName);
		return this.httppost("updimg", map, request);
	}
	// 修改个人信息
	@RequestMapping(value="/updUser",method=RequestMethod.POST,produces = "application/json; charset=utf-8")
	@ResponseBody
	public String updUser(@RequestParam Map<String,Object> map, HttpServletRequest request) throws Exception {
		if(StaticMapUtil.getmap().get("session")!=null) {
			map.put("sessionId", StaticMapUtil.getmap().get("session"));
		}else {
			map.put("cuid", "9527");
		}
		return this.httppost("upduserinfo", map, request);
	}
	// 获取用户地址
	@RequestMapping(value="/address",method=RequestMethod.GET,produces = "application/json; charset=utf-8")
	@ResponseBody
	public String address(@RequestParam Map<String,Object> map, HttpServletRequest request) throws Exception {
		if(StaticMapUtil.getmap().get("session")!=null) {
			map.put("sessionId", StaticMapUtil.getmap().get("session"));
		}else {
			map.put("cuid", "9527");
		}
		log.error(map);
		return this.httppost("queryaddress", map, request);
	}
	// 修改用户地址
	@RequestMapping(value="/updaddress",method=RequestMethod.POST,produces = "application/json; charset=utf-8")
	@ResponseBody
	public String updaddress(@RequestParam Map<String,String> map, HttpServletRequest request) throws Exception {
		log.error(map);
		return this.httppost("updadd", map, request);
	}
	// 新增用户地址
	@RequestMapping(value="/addAddress",method=RequestMethod.POST,produces = "application/json; charset=utf-8")
	@ResponseBody
	public String addAddress(@RequestParam Map<String,Object> map, HttpServletRequest request) throws Exception {
		if(StaticMapUtil.getmap().get("session")!=null) {
			map.put("sessionId", StaticMapUtil.getmap().get("session"));
		}else {
			map.put("cuid", "9527");
		}
		log.error(map);
		return this.httppost("insertadd", map, request);
	}
	
	// 获取个人订单
	@RequestMapping(value="/getuserorder",method=RequestMethod.POST,produces = "application/json; charset=utf-8")
	@ResponseBody
	public String getuserorder(@RequestParam Map<String,Object> map, HttpServletRequest request) throws Exception {
		if(StaticMapUtil.getmap().get("session")!=null) {
			map.put("sessionId", StaticMapUtil.getmap().get("session"));
		}else {
			map.put("cuid", "9527");
		}
		return this.httppost("order", map, request);
	}
	
	// 新增留言
	@RequestMapping(value="/insmsg",method=RequestMethod.POST,produces = "application/json; charset=utf-8")
	@ResponseBody
	public String insmsg(@RequestParam Map<String,Object> map, HttpServletRequest request) throws Exception {
		if(StaticMapUtil.getmap().get("session")!=null) {
			map.put("sessionId", StaticMapUtil.getmap().get("session"));
		}else {
			map.put("cuid", "9527");
		}
		log.error(map);
		return this.httppost("leaveMessage", map, request);
	}
	
	// 用户提交服务完成
	@RequestMapping(value="/orderstateend",method=RequestMethod.POST,produces = "application/json; charset=utf-8")
	@ResponseBody
	public String orderstate(@RequestParam Map<String,Object> map, HttpServletRequest request) throws Exception {
		if(StaticMapUtil.getmap().get("session")!=null) {
			map.put("sessionId", StaticMapUtil.getmap().get("session"));
		}else {
			map.put("cuid", "9527");
		}
		if(map.get("page")==null) {
			map.put("page", "1");
		}
		return this.httppost("orderstate", map, request);
	}
	
	// 查看留言、评价
	@RequestMapping(value="/assess",method=RequestMethod.POST,produces = "application/json; charset=utf-8")
	@ResponseBody
	public String assess(@RequestParam Map<String,Object> map, HttpServletRequest request) throws Exception {
		if(StaticMapUtil.getmap().get("session")!=null) {
			map.put("sessionId", StaticMapUtil.getmap().get("session"));
		}else {
			map.put("cuid", "9527");
		}
//		map.put("cuid", "9527");
		return this.httppost("assess", map, request);
	}

	
	//服务人员信息
	@RequestMapping(value="/querySingle",method=RequestMethod.POST,produces = "application/json; charset=utf-8")
	@ResponseBody
	public String querySingle(@RequestParam Map<String,String> map , HttpServletRequest request) throws Exception{
		log.error(map);
		if(map.isEmpty()) {
			return this.httppost("querySingle", null, request);
		}else {
			return this.httppost("queryinemp", map, request);
		}
	}

	
	
	// 页面跳转
	@RequestMapping(value="/personal") // ,produces = "application/json; charset=utf-8",method=RequestMethod.GET
	public String topersonal() { //@RequestParam Map<String,String> map) throws Exception {
		//map.put("cuid", "9527");
		return "/i.html";
	}
	
	@RequestMapping(value="/staff") // ,produces = "application/json; charset=utf-8",method=RequestMethod.GET
	public String tocorehub() { //@RequestParam Map<String,String> map) throws Exception {
		//map.put("cuid", "9527");
		return "/g.html";
	}
	
	@RequestMapping(value="/qiye") // ,produces = "application/json; charset=utf-8",method=RequestMethod.GET
	public String toqiye() { //@RequestParam Map<String,String> map) throws Exception {
		//map.put("cuid", "9527");
		return "/q.html";
	}
	
	@RequestMapping(value="/msgg") // ,produces = "application/json; charset=utf-8",method=RequestMethod.GET
	public String toassess() { //@RequestParam Map<String,String> map) throws Exception {
		//map.put("cuid", "9527");
		return "/msg.html";
	}
	
	@RequestMapping(value="/errooo") // ,produces = "application/json; charset=utf-8",method=RequestMethod.GET
	public String error() { //@RequestParam Map<String,String> map) throws Exception {
		//map.put("cuid", "9527");
		return "/error.html";
	}
	
	// 放入sessionid
	@RequestMapping(value="/setsessionid",method=RequestMethod.POST,produces = "application/json; charset=utf-8")
	@ResponseBody
	public void setsessionid(@RequestParam Map<String,String> map , HttpServletRequest request) throws Exception {
		//map.put("cuid", "9527");
		if(map.get("sessionId")!=null) {
			StaticMapUtil.getmap().put("session", map.get("sessionId"));
			System.err.println("获取到接口发送的用户!"+map.get("sessionId"));
		}else{
			System.err.println("没有获取到接口发送的值?");
		};
	}
	
	// Test测试 接口
	@RequestMapping(value="/sing",method=RequestMethod.POST,produces = "application/json; charset=utf-8")
	@ResponseBody
	public String sing(@RequestParam Map<String,String> map , HttpServletRequest request) throws Exception {
		//map.put("cuid", "9527");
		return this.httppost("querySingle", null ,request);
	}
}
